﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

using Utilities.StringLib;

namespace MyVectorizer
{
    public class WordStatistics
    {
        Dictionary<string, double> wordDict;
        WeightVector weightVector;

        /// <summary>
        /// Wordrater
        /// </summary>
        public WeightVector WordRating
        {
            get
            {
                return weightVector;
            }
            set
            {
                weightVector = value;
            }
        }

        /// <summary>
        /// The dictionary that contains the number of appearance of a term.
        /// </summary>
        public Dictionary<string, double> WordDict
        {
            get
            {
                return wordDict;
            }
            set
            {
                wordDict = value;
            }
        }

        public WordStatistics()
        {
            wordDict = new Dictionary<string, double>();
            weightVector = new WeightVector();
        }

        public WordStatistics(WeightVector wr)
        {
            wordDict = new Dictionary<string, double>();
            this.weightVector = wr;
        }
        /// <summary>
        /// Make the dictionary for a page
        /// </summary>
        /// <param name="annotatedKeywords">annotated keywords of a page</param>
        /// <param name="splitWebtagList">tags which were split of a page</param>
        /// <param name="webpageMetaList">list of meta keyword of a page</param>
        /// <param name="webpageSplitTitle">split title of a page</param>
        public void MakeDictionary(List<string> annotatedKeywords, List<Webtag> splitWebtagList, List<string> webpageMetaList, string webpageSplitTitle)
        {        
            foreach (string keyword in annotatedKeywords)
            {
                double value = 0.0;
                // if it appears in meta
                if (webpageMetaList.Contains(keyword))
                {
                    value += weightVector.MetaTextWeight;
                }
                // if it appears in title                
                string[] splitStr = Utilities.StringLib.ProcessString.ProcessSplitText(webpageSplitTitle);
                foreach (string str in splitStr)
                {
                    if (str.Equals(keyword))
                    {
                        value += weightVector.TitleTextWeight;
                    }
                }               
                // if it apears in normal tag
                foreach (Webtag wtag in splitWebtagList)
                {
                    splitStr = Utilities.StringLib.ProcessString.ProcessSplitText(wtag.Text);
                    foreach (string str in splitStr)
                    {
                        if (str.Equals(keyword))
                        {
                            value += weightVector.NormalTextWeight;
                        }
                    }
                }
                // add to dictionary
                wordDict.Add(keyword, value);
            }            
        }

        /// <summary>
        /// Make vector which represents the webpage
        /// </summary>
        /// <param name="webpageID">Webpage ID</param>
        /// <param name="annotatedKeywords">List of common term</param>
        /// <returns>The page vector</returns>
        public Vector MakeVector(int webpageID, List<string> commonTerms)
        {
            Vector result = new Vector();
            result.WebpageID = webpageID;

            for (int i = 0; i < commonTerms.Count; i++)
            {
                if (wordDict.ContainsKey(commonTerms[i]) == true)
                {
                    result.Value.Add(wordDict[commonTerms[i]]);
                }
                else
                {
                    result.Value.Add(0.0);
                }
            }

            return result;
        }
    }
}
